package com.chuqiyun.authorization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuqiyun.authorization.dao.BillDao;
import com.chuqiyun.authorization.entity.Bill;
import com.chuqiyun.authorization.service.BillService;
import org.springframework.stereotype.Service;

/**
 * 账单(Bill)表服务实现类
 *
 * @author mryunqi
 * @since 2023-04-19 17:06:37
 */
@Service("billService")
public class BillServiceImpl extends ServiceImpl<BillDao, Bill> implements BillService {
    /**
    * @Author: mryunqi
    * @Description: 创建支付账单
    * @DateTime: 2023/4/22 14:20
    * @Params: String uuid 用户唯一UUID
    * @Params: String traceNo 系统生成订单号
    * @Params: String paymentType 支付类型别名
    * @Params: Boolean money 变动金额
    * @Params: Boolean balance 余额
    * @Return int
    */
    @Override
    public int insertPayBill(String uuid, String traceNo,
                              String paymentType,Double money,
                              Double balance){
        Bill bill = new Bill();
        bill.setUserUuid(uuid);
        bill.setPaymentType(paymentType);
        bill.setMoney(money);
        bill.setOrderId(traceNo);
        bill.setBalance(balance);
        bill.setCreateDate(System.currentTimeMillis());
        return this.baseMapper.insert(bill);
    }

    /**
    * @Author: mryunqi
    * @Description: 获取指定订单号的账单
    * @DateTime: 2023/4/22 15:31
    * @Params: String orderId 系统订单号
    * @Return Bill
    */
    @Override
    public Bill getBillByOrderId(String orderId){
        QueryWrapper<Bill> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        return this.getOne(queryWrapper);
    }

    /**
    * @Author: mryunqi
    * @Description: 支付成功账单更新
    * @DateTime: 2023/4/22 15:31
    * @Params: Bill
    * @Return int
    */
    @Override
    public int updatePayBillSuccess(Bill bill){
        bill.setState(1);
        bill.setBalance(bill.getBalance()+bill.getMoney());
        return this.baseMapper.updateById(bill);
    }

    /**
    * @Author: mryunqi
    * @Description: 获取指定页所有账单
    * @DateTime: 2023/4/22 19:58
     * @Params: Integer page 页数
     * @Params: Integer size 每页数据
    * @Return Page<Bill>
    */
    @Override
    public Page<Bill> getBillList(Integer page, Integer size) {
        Page<Bill> billPage = new Page<>(page, size);
        return this.page(billPage);
    }

    @Override
    public Page<Bill> getBillList(Integer page, Integer size, QueryWrapper<Bill> queryWrapper) {
        Page<Bill> billPage = new Page<>(page, size);
        return this.page(billPage,queryWrapper);
    }

}

